﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections.Specialized;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using Utils;
using Maticsoft.DBUtility;

/// <summary>
///BaseUserControl 的摘要说明
/// </summary>
public class BaseUserControl : System.Web.UI.UserControl
{
    // Fields
    private Maticsoft.Model.System _assystem = null;
    private NameValueCollection _assystemarr = null;
    private Maticsoft.Model.UserInfo _asuser = null;
    private NameValueCollection _asuserarr = null;
    private Maticsoft.Model.Category _city = null;
    private DataTable _citylist = new DataTable();
    private Maticsoft.Model.Team _currentteam = null;
    private bool? _ismanager = null;
    private Maticsoft.Model.Partner _partner = null;
    private Maticsoft.Model.Team _teambase = null;
    public BasePage b = new BasePage();
    public string errtext = string.Empty;
    public string suctext = string.Empty;

    // Methods
    public string ImagePath()
    {
        string str = this.WebRoot + "upfile/css/i/";
        if ((this.ASSystem != null) && (this.ASSystem.skintheme != string.Empty))
        {
            return (this.WebRoot + "upfile/theme/" + this.ASSystem.skintheme + "/css/i/");
        }
        return (this.WebRoot + "upfile/css/i/");
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if (base.Session["err"] != null)
        {
            switch (Helper.GetInt(base.Session["type"], 1))
            {
                case 1:
                    this.errtext = base.Session["err"].ToString();
                    break;

                case 2:
                    this.suctext = base.Session["err"].ToString();
                    break;
            }
            base.Session.Remove("err");
            base.Session.Remove("type");
        }
    }

    public void SetError(string errtext)
    {
        base.Session["err"] = errtext;
        base.Session["type"] = 1;
    }

    public void SetSuccess(string successtext)
    {
        base.Session["err"] = successtext;
        base.Session["type"] = 2;
    }

    public string Share_douban(Maticsoft.Model.Team team)
    {
        string str = string.Empty;
        str = "http://www.douban.com/recommend/?";
        if (team != null)
        {
            return ((str + "url=" + base.Server.UrlEncode(string.Concat(new object[] { this.WWWprefix, "team.aspx?id=", team.Id, "&r=", this.ASUserArr["id"] }))) + "&title=" + base.Server.UrlEncode(this.WWWprefix + team.Title));
        }
        return ((str + "url=" + base.Server.UrlEncode(this.WWWprefix + "r.aspx?r=" + this.ASUserArr["id"])) + "&title=" + base.Server.UrlEncode(this.ASSystemArr["sitename"] + "(" + this.WWWprefix + ")"));
    }

    public string Share_kaixin(Maticsoft.Model.Team team)
    {
        string str = string.Empty;
        str = "http://www.kaixin001.com/repaste/share.php?rurl=";
        if (team != null)
        {
            return (((str + base.Server.UrlEncode(string.Concat(new object[] { this.WWWprefix, "team.aspx?id=", team.Id, "&r=", this.ASUserArr["id"] }))) + "&rtitle=" + base.Server.UrlEncode(team.Title)) + "&rcontent=" + base.Server.UrlEncode(team.Summary));
        }
        return (((str + base.Server.UrlEncode(this.WWWprefix + "r.aspx?r=" + this.ASUserArr["id"])) + "&rtitle=" + base.Server.UrlEncode(this.ASSystemArr["sitename"] + "(" + this.WWWprefix + ")")) + "&rcontent=" + base.Server.UrlEncode(this.ASSystemArr["sitename"] + "(" + this.WWWprefix + ")"));
    }

    public string Share_mail(Maticsoft.Model.Team team)
    {
        string[] strArray;
        string body = string.Empty;
        string str2 = string.Empty;
        if (team == null)
        {
            body = this.ASSystemArr["sitename"] + "(" + this.WWWprefix + ")";
            strArray = new string[] { "发现一好网站--" + this.ASSystemArr["sitename"] + "，他们每天组织一次团购，超值!" };
            str2 = string.Join("\r\n", strArray);
        }
        else
        {
            body = "有兴趣吗：" + team.Title;
            strArray = new string[] { "发现一好网站--" + this.ASSystemArr["sitename"] + "，他们每天组织一次团购，超值!", "今天的团购是：" + team.Title, "我想你会感兴趣的：", string.Concat(new object[] { this.WWWprefix, "team.aspx?id=", team.Id, "&r=", this.ASUserArr["id"] }) };
            str2 = string.Join("\r\n", strArray);
        }
        body = this.UTF8ToGB2312(body);
        str2 = this.UTF8ToGB2312(str2);
        return ("mailto:?subject=" + body + "&body=" + str2);
    }

    public string Share_renren(Maticsoft.Model.Team team)
    {
        string str = string.Empty;
        if (team != null)
        {
            str = ("link=" + base.Server.UrlEncode(string.Concat(new object[] { this.WWWprefix, "team.aspx?id=", team.Id, "&r=", this.ASUserArr["id"] }))) + "&title=" + base.Server.UrlEncode(team.Title);
        }
        else
        {
            str = ("link=" + base.Server.UrlEncode(this.WWWprefix + "r.aspx?id=" + this.ASUserArr["id"])) + "&title=" + base.Server.UrlEncode(this.ASSystemArr["sitename"] + "(" + this.WWWprefix + ")");
        }
        return ("http://share.renren.com/share/buttonshare.do?" + str);
    }

    public string Share_sina(Maticsoft.Model.Team team)
    {
        string str = string.Empty;
        str = "http://v.t.sina.com.cn/share/share.php?";
        if (team != null)
        {
            return ((str + "url=" + base.Server.UrlEncode(string.Concat(new object[] { this.WWWprefix, "team.aspx?id=", team.Id, "&r=", this.ASUserArr["id"] }))) + "&title=" + base.Server.UrlEncode(this.WWWprefix + team.Title));
        }
        return ((str + "url=" + base.Server.UrlEncode(this.WWWprefix + "r.aspx?r=" + this.ASUserArr["id"])) + "&title=" + base.Server.UrlEncode(this.ASSystemArr["sitename"] + "(" + this.WWWprefix + ")"));
    }

    public string UTF8ToGB2312(string body)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(body);
        body = HttpUtility.UrlEncode(Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding("gb2312"), bytes));
        return body;
    }

    // Properties
    public Maticsoft.Model.System ASSystem
    {
        get
        {
            if (this._assystem == null)
            {
                this._assystem = new Maticsoft.BLL.System().GetModel(1);
            }
            return this._assystem;
        }
        set
        {
            this._assystem = value;
        }
    }

    public NameValueCollection ASSystemArr
    {
        get
        {
            if (this._assystemarr == null)
            {
                this._assystemarr = Helper.GetObjectProtery(this.ASSystem);
                if (this._assystemarr == null)
                {
                    this._assystemarr = new NameValueCollection();
                }
            }
            return this._assystemarr;
        }
    }

    public Maticsoft.Model.UserInfo AsUser
    {
        get
        {
            if ((this._asuser == null) && this.Page.User.Identity.IsAuthenticated)
            {
                List<Maticsoft.Model.UserInfo> modelList = new Maticsoft.BLL.UserInfo().GetModelList("Username='" + this.Page.User.Identity.Name + "'");
                if (modelList.Count > 0)
                {
                    this._asuser = modelList[0];
                }
            }
            return this._asuser;
        }
    }

    public NameValueCollection ASUserArr
    {
        get
        {
            if (this._asuserarr == null)
            {
                this._asuserarr = Helper.GetObjectProtery(this.AsUser);
                if (this._asuserarr == null)
                {
                    this._asuserarr = new NameValueCollection();
                }
            }
            return this._asuserarr;
        }
    }

    public DataTable CityList
    {
        get
        {
            if (this._citylist.Rows.Count == 0)
            {
                this._citylist = DbHelperSQL.SelectByFilter("*", "Zone='" + WebSiteHelper.GetCatalogName(CatalogType.city) + "' and Display='Y'", "Sort_order desc", "category");
            }
            return this._citylist;
        }
    }

    public Maticsoft.Model.Category CurrentCity
    {
        get
        {
            if (this._city == null)
            {
                string cookieValue = CookieHelper.GetCookieValue("cityid");
                if (cookieValue == string.Empty)
                {
                    Maticsoft.BLL.Category category = new Maticsoft.BLL.Category();
                    DataSet set = category.GetList(1, "zone='" + WebSiteHelper.GetCatalogName(CatalogType.city) + "'", "Sort_order desc");
                    if ((set.Tables.Count > 0) && (set.Tables[0].Rows.Count > 0))
                    {
                        List<Maticsoft.Model.Category> list = category.DataTableToList(set.Tables[0]);
                        if (list.Count > 0)
                        {
                            this._city = list[0];
                        }
                    }
                }
                else
                {
                    this._city = new Maticsoft.BLL.Category().GetModel(Helper.GetInt(cookieValue, 0));
                }
            }
            return this._city;
        }
    }

    public Maticsoft.Model.Partner CurrentPartner
    {
        get
        {
            if ((this._partner == null) && (this.b.GetCookie("partnerid") != string.Empty))
            {
                this._partner = new Maticsoft.BLL.Partner().GetModel(Helper.GetInt(this.b.GetCookie("partnerid"), 0));
            }
            return this._partner;
        }
    }

    public Maticsoft.Model.Team CurrentTeam
    {
        get
        {
            if (this._currentteam == null)
            {
                DataTable dt = new DataTable();
                if (this.CurrentCity == null)
                {
                    dt = DbHelperSQL.SelectByFilter("*", "", "", "(select top 1 * from team where  Begin_time<='" + DateTime.Now.ToString() + "' and End_time>='" + DateTime.Now.ToString() + "'  order by  sort_order desc,id asc) as t1");
                }
                else
                {
                    dt = DbHelperSQL.SelectByFilter("*", "", "", string.Concat(new object[] { "(select top 1 * from team where (City_id=", this.CurrentCity.Id, " or city_id=0) and  Begin_time<='", DateTime.Now.ToString(), "' and End_time>='", DateTime.Now.ToString(), "'  order by city_id desc, sort_order desc,id asc ) as t1" }));
                }
                if (dt.Rows.Count > 0)
                {
                    List<Maticsoft.Model.Team> list = new Maticsoft.BLL.Team().DataTableToList(dt);
                    if (list.Count > 0)
                    {
                        this._currentteam = list[0];
                    }
                }
            }
            return this._currentteam;
        }
    }

    public bool IsLogin
    {
        get
        {
            bool flag = false;
            if (this.Page.User.Identity.IsAuthenticated)
            {
                flag = true;
            }
            return flag;
        }
    }

    public bool IsManage
    {
        get
        {
            if (!this._ismanager.HasValue)
            {
                if ((this.AsUser != null) && (this.AsUser.Manager.ToUpper() == "Y"))
                {
                    this._ismanager = true;
                }
                if (!this._ismanager.HasValue)
                {
                    this._ismanager = false;
                }
            }
            return this._ismanager.Value;
        }
    }

    public Maticsoft.Model.Team Teambase
    {
        get
        {
            return this._teambase;
        }
        set
        {
            this._teambase = value;
        }
    }

    public string UserName
    {
        get
        {
            string name = string.Empty;
            if (this.Page.User.Identity.IsAuthenticated)
            {
                name = this.Page.User.Identity.Name;
            }
            return name;
        }
    }

    public string WebRoot
    {
        get
        {
            return this.Page.ResolveUrl("~/");
        }
    }

    public string WWWprefix
    {
        get
        {
            Regex regex = new Regex("http://(.+?)/");
            return regex.Match(base.Request.Url.AbsoluteUri).Value;
        }
    }

}